<?
  set_time_limit(4500);
  /* Se asegura que el archivo es incluido por el index */
    defined( 'LOAD_MODULE_ACTION' ) or die( 'No esta permitido el acceso directo a ' . __FILE__ );
	/* Cuando se hace el include del archivo automaticamente crea un objeto y lo guarda en una variable temporal */
		$this->oTmpObject = new Menu_exe( $this );

	class Menu_exe extends Exe
	{
	function Menu_exe( &$oModule )
		{
			$this->Exe( $oModule );
		}
	function updateStar()
		{
			$ncMenu = getParam( 'ncMenu' );
			$bEstrella = getParam( 'bEstrella' );
			if ( $ncMenu )
			{
				$this->oDatabase->update( "gtMenu", "bEstrella", $bEstrella, "WHERE", "ncMenu", $ncMenu );
			}
		}
	function updateTag()
		{
			$ncMenu = getParam( 'ncMenu' );
			$ncRotulo = getParam( 'ncRotulo' );
			$cNewItem = getParam( 'newItem' );
			if ( $cNewItem )
			{
				$ncRotulo = $this->oDatabase->nextKey( "gtRotuloMenu", "ncRotulo" );
				$bOk = $this->oDatabase->insert( "gtRotuloMenu",
										"ncRotulo", $ncRotulo, "cdRotulo", $cNewItem );
			}
			if ( $ncMenu && $ncRotulo )
			{
				$bOk = $this->oDatabase->insert( "gtMenuRotulo",
										"ncRotulo", $ncRotulo, "ncMenu", $ncMenu );
			}
		}
		function replicarMenu()
		{
			$oForm = $this->oModule->loadClass( 'Replicar_componentes_frm' );
			extract( $oForm->getFieldArray() );
			$cSql = "select ncMenu, ncComponente, nOrden, nPanel, cParametros  from gtMenuComponente  WHERE ncMenu = $ncMenu AND nPanel = $nPanel";
			$aRows= $this->oDatabase->getMatrix( $cSql );
			$ncOrden = $this->oDatabase->nextKey( "gtMenuComponente", "ncOrden", "ncMenu = $ncMenuReplica" );
			foreach($aRows as $aRow )
			{
				$this->oDatabase->insert ( "gtMenuComponente", "ncMenu", $ncMenuReplica, "ncOrden", $ncOrden, "ncComponente", $aRow[ncComponente], "nOrden", $aRow[nOrden], "nPanel", $nPanel, "cParametros",  $aRow['cParametros'] );
				$ncOrden++;
			}
			$cSql = "select a.ncContenido, a.nOrden from gtMenuContenido as a JOIN gtContenido as b ON a.ncContenido = b.ncContenido WHERE ncMenu = $ncMenu AND ncTipoContenido not in (9,17) ";
			$aRows= $this->oDatabase->getMatrix( $cSql );
			foreach($aRows as $aRow )
				$this->oDatabase->insert ( "gtMenuContenido", "ncMenu", $ncMenuReplica,  "ncContenido", $aRow[ncContenido],  "nOrden", $aRow[nOrden] );
		}
		// AGREGAR_MENU
		function insert()
		{
			// Convierte a variables los datos del Formulario asociado a la accion
			$oForm = $this->oModule->loadClass( 'Menu_frm' );
			extract( $oForm->getFieldArray() );
			// Busca el siguiente id
			$ncMenu      = $this->oDatabase->nextKey("gtMenu", "ncMenu"  );
			
			//if( $nModo == 4 && substr($cUrl, 0, 1) != '?' && substr($cUrl, 0, 7) != 'http://' ) $cUrl = 'http://' .$cUrl;
			if( $nModo == 3 )
			{
				$cUrl = str_replace('&', '&amp;', $cUrl);
				if ( $cListaParam )
				{
					foreach( explode( ',', $cListaParam ) as $cParametro )
					{
						$cValue = getParam($cParametro);
						if ( $cValue  ) $cUrl .= '&amp;' . $cParametro . '=' . $cValue  ;
					}
				}
			}
			$this->oDatabase->insert("gtMenu"
								,"ncMenu", $ncMenu
								,"ncMenuPadre", $ncMenuPadre
								,"cdMenu", $cdMenu
								,"nVista", $nVista
								,"nModo", $nModo
								,"nOrden", $nOrden
								,"cUrl", $cUrl
								,"cCamino", armarCamino( $this->oDatabase, $ncMenuPadre ) 
								,"cDescripcion", $cDescripcion
								,"bExpandido", $bExpandido
								,"ncEstadoPublicacion", $ncEstadoPublicacion 
								,"dAlta", date('Y-m-d')
								,"ncAlta", $this->oModule->oUserSession->getUser()
								,"dModificacion", date('Y-m-d')
								,"ncModificacion", $this->oModule->oUserSession->getUser());
			// Agrega los rotulos
			$aRotulo = $oForm->aFields['aRotulo']->getValue();
			foreach ( (array) $aRotulo as $ncRotulo )
				$this->oDatabase->insert( "gtMenuRotulo", "ncRotulo", $ncRotulo,"ncMenu", $ncMenu );
			$oForm->reset();
		}

		// ELIMINAR_MENU
		function delete()
		{
			$ncMenu = getParam( 'ncMenu' );
			$this->oDatabase->delete("gtMenu","ncMenu", $ncMenu );
		}

		function updateOrder()
		{
			$nCambiarOrden = getParam('nCambiarOrden');
			$ncMenuPadre = getParam('ncMenuPadre');
			$ncMenu = getParam('ncMenu');
			$nOrden = getParam('nOrden');
			if ( $nCambiarOrden  && $ncMenu )
			{
				if ( $nCambiarOrden == -1 )
					list( $ncMenuCbo, $nOrdenCbo ) = $this->oDatabase->getArray( "SELECT ncMenu, nOrden FROM gtMenu WHERE ncMenuPadre = $ncMenuPadre AND nOrden < $nOrden ORDER BY nOrden " , 0, 1);
				else
					list( $ncMenuCbo, $nOrdenCbo ) = $this->oDatabase->getArray( "SELECT ncMenu, nOrden FROM gtMenu WHERE ncMenuPadre = $ncMenuPadre AND nOrden > $nOrden ORDER BY nOrden ", 0, 1 );
				if ( $ncMenuCbo )
				{
					$this->oDatabase->update("gtMenu"
									,"nOrden", $nOrden
									,"WHERE"
									 ,"ncMenu", $ncMenuCbo);
				} else
				{ // Si no hay ni mayor ni menor => pone 1 (minimo)  o la cantidad de registros como maximo
					if ( $nCambiarOrden == -1 )
						$nOrdenCbo = 1;
					else
						$nOrdenCbo = $this->oDatabase->getData( "SELECT count(*) FROM gtMenu WHERE ncMenuPadre = $ncMenuPadre " );
				}
				$this->oDatabase->update("gtMenu"
								,"nOrden", $nOrdenCbo
								,"WHERE"
								 ,"ncMenu", $ncMenu);
			}
		}
		// MODIFICAR_MENU
		function update()
		{
			// Convierte a variables los datos del Formulario asociado a la accion
			$oForm = $this->oModule->loadClass( 'Menu_frm' );
			extract( $oForm->getFieldArray() );
			//if( $nModo == 4 && substr($cUrl, 0, 1) != '?' && substr($cUrl, 0, 7) != 'http://' ) $cUrl = 'http://' .$cUrl;
			if( $nModo == 3 )
			{
				$cUrl = str_replace('&', '&amp;', $cUrl);
				if ( $cListaParam )
				{
					foreach( explode( ',', $cListaParam ) as $cParametro )
					{
						$cValue = getParam($cParametro);
						if ( $cValue  ) $cUrl .= '&amp;' . $cParametro . '=' . $cValue  ;
					}
				}
			}
			$this->oDatabase->update("gtMenu"
								,"ncMenuPadre", $ncMenuPadre
								,"cdMenu", $cdMenu
								,"nVista", $nVista
								,"nModo", $nModo
								,"nOrden", $nOrden
								,"cUrl", $cUrl
								,"cCamino", armarCamino( $this->oDatabase, $ncMenuPadre ) 
								,"cDescripcion", $cDescripcion
								,"bExpandido", $bExpandido
								,"ncEstadoPublicacion", $ncEstadoPublicacion
								,"dModificacion", date('Y-m-d')
								,"ncModificacion", $this->oModule->oUserSession->getUser()								
								,"WHERE"
								 ,"ncMenu", $ncMenu);
			$this->oDatabase->delete( "gtMenuRotulo", "ncMenu", $ncMenu );
			$aRotulo = $oForm->aFields['aRotulo']->getValue();
			foreach ( (array) $aRotulo as $ncRotulo )
				$this->oDatabase->insert( "gtMenuRotulo", "ncRotulo", $ncRotulo,"ncMenu", $ncMenu );
			$oForm->reset();
		}
	}
?>